package com.aaa.controller;

import com.aaa.entity.Leadingusers;
import com.aaa.service.LeadingUsersService;
import com.aaa.util.DefaultMsg;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.UUID;

@RestController
public class LeadingUsersController {
    @Resource
    private LeadingUsersService leadingUsersService;

    //保存图片的文件夹名称
    private static final String INM_PATH = "E:/images/";

    @RequestMapping("/addLeadingUsers")
    public DefaultMsg addLeadingUsers(Leadingusers leadingusers) throws Exception {
        return leadingUsersService.addLeadingUsers(leadingusers);
    }
    @RequestMapping("/checkUsername")
    public DefaultMsg checkUsername(String username){
        DefaultMsg defaultMsg = leadingUsersService.checkUsername(username);
        return defaultMsg;
    }
    @RequestMapping("/upload")
    public DefaultMsg upload(MultipartFile file) {
        DefaultMsg defaultMsg = new DefaultMsg();
//      使用UUID为文件生成一个唯一的名称
        String fileName = UUID.randomUUID().toString();
        File dest = new File(INM_PATH + fileName);
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("上传失败");
        }
        defaultMsg.setTarget(fileName);
        return defaultMsg;
    }
    @RequestMapping("/updateLeadingusers")
    public DefaultMsg updateLeadingusers(Leadingusers leadingusers){
        DefaultMsg defaultMsg = leadingUsersService.updateLeadingusers(leadingusers);
        return defaultMsg;
    }
    @RequestMapping("/updatePassword")
    public DefaultMsg updatePassword(String oldPassword, String password, Authentication authentication){
        String lid = authentication.getPrincipal().toString();
        DefaultMsg defaultMsg = leadingUsersService.updatePassword(oldPassword,password, Integer.valueOf(lid));
        return defaultMsg;
    }
}
